/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <KLocale>

#include <QtCore/QDebug>
#include <QtGui/QHBoxLayout>

#include <kore.h>
#include <ui/player/seekwidget.h>

SeekWidget::~SeekWidget() {
}

SeekWidget::SeekWidget(QWidget *parent) : QWidget(parent), userHoldsSlider(false) {

    player = Kore::instance().player();

    currentLabel = new QLabel(this);
    currentLabel->setToolTip(i18n("Current time"));
    remainingLabel = new QLabel(this);
    remainingLabel->setToolTip(i18n("Remaining time"));
    seekSlider = new QSlider(this);
    seekSlider->setOrientation(Qt::Horizontal);
    seekSlider->setMinimum(0);
    seekSlider->setTracking(false);
    seekSlider->setTickInterval(1000);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(currentLabel);
    layout->addWidget(seekSlider);
    layout->addWidget(remainingLabel);

    connect(player, SIGNAL(seekableChanged(bool)), SLOT(seekableChanged(bool)));
    connect(player, SIGNAL(tick(qint64)), SLOT(tick(qint64)));
    connect(player, SIGNAL(totalTimeChanged(qint64)), SLOT(totalTimeChanged(qint64)));
    connect(player, SIGNAL(sourceChanged()), SLOT(updateEverything()));
    connect(seekSlider, SIGNAL(sliderPressed()), SLOT(sliderPressed()));

    updateEverything();
}

void SeekWidget::seekableChanged(bool isSeekable) {
    if (isSeekable) {
        seekSlider->setEnabled(true);
    }
    else {
        seekSlider->setMaximum(100);
        seekSlider->setValue(0);
        seekSlider->setEnabled(false);
    }
}

void SeekWidget::tick(qint64 time) {
    Q_UNUSED(time)
    updateEverything();
}

void SeekWidget::totalTimeChanged(qint64 newTotalTime) {
    Q_UNUSED(newTotalTime);
    updateEverything();
}

QString SeekWidget::formatTime(qint64 time) {
    timeFormatter.setHMS((time / 3600000), (time / 60000) % 60, (time / 1000) % 60);
    if (timeFormatter.hour() > 0) {
        return timeFormatter.toString("HH:mm:ss");
    }
    else {
        return timeFormatter.toString("mm:ss");
    }
}

void SeekWidget::sliderPressed() {
    userHoldsSlider = true;
    connect(seekSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
}

void SeekWidget::sliderValueChanged(int value) {
    userHoldsSlider = false;
    disconnect(seekSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
    player->seek(value);
    // TODO update labels too?
}

void SeekWidget::updateEverything() {
    qint64 time = player->time();
    qint64 totalTime = player->totalTime();
    currentLabel->setText(formatTime(time));
    remainingLabel->setText("-" + formatTime(totalTime - time));
    if (!userHoldsSlider) {
        seekSlider->setMaximum(totalTime);
        seekSlider->setValue(time);
    }
}
